package com.xiaoshuidi.cloud.module.contract.service.contract.impl;

import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractExtrasDataCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractExtrasDataUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.contract.ContractExtrasDataConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractExtrasDataDO;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractExtrasDataMapper;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractExtrasDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.contract.enums.ErrorCodeConstants.*;

/**
 * 合同杂费 Service 实现类
 *
 * @author 合房租赁
 */
@Service
public class ContractExtrasDataServiceImpl implements ContractExtrasDataService {

    @Resource
    private ContractExtrasDataMapper extrasDataMapper;

    @Override
    public Long createExtrasData(ContractExtrasDataCreateReqVO createReqVO) {
        // 插入
        ContractExtrasDataDO extrasData = ContractExtrasDataConvert.INSTANCE.convert(createReqVO);
        extrasDataMapper.insert(extrasData);
        // 返回
        return extrasData.getId();
    }

    @Override
    public List<ContractExtrasDataDO> getExtrasDatalist(Long contractId) {
        return extrasDataMapper.getExtrasDatalist(contractId);
    }

    @Override
    public void createExtrasDataList(List<ContractExtrasDataDO> extrasData) {
        // 插入
        extrasDataMapper.insertBatch(extrasData);
    }

    @Override
    public void updateExtrasData(ContractExtrasDataUpdateReqVO updateReqVO) {
        // 校验存在
        validateExtrasDataExists(updateReqVO.getId());
        // 更新
        ContractExtrasDataDO updateObj = ContractExtrasDataConvert.INSTANCE.convert(updateReqVO);
        extrasDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteExtrasData(Long id) {
        // 校验存在
        validateExtrasDataExists(id);
        // 删除
        extrasDataMapper.deleteById(id);
    }

    private void validateExtrasDataExists(Long id) {
        if (extrasDataMapper.selectById(id) == null) {
            throw exception(EXTRAS_DATA_NOT_EXISTS);
        }
    }

    @Override
    public ContractExtrasDataDO getExtrasData(Long id) {
        return extrasDataMapper.selectById(id);
    }

    public int deleteByContractId(Long contractId) {
        return extrasDataMapper.deleteByContractId(contractId);
    }

}
